package com.lifeng.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lifeng.ThymeleafServiceStart;
import com.lifeng.bean.User;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by lifeng on 2019/5/9.
 */
@Controller
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);
    @RequestMapping("/test")
    public String addUI(Model model){
        model.addAttribute("name","张三");
        return "demo";
    }

    @RequestMapping("/testobj")
    public String addObject(Model model){
        User user = new User();
        user.setId("111");
        user.setName("张三");
        user.setAge(12);
        user.setBirthday(new Date());
        model.addAttribute("user",user);
        return "demo";
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<User> list = new ArrayList<User>();
        User user = new User();
        user.setId("1");
        user.setName("张三");
        user.setAge(12);
        user.setBirthday(new Date());
        user.setSex(1);
        list.add(user);
        User user1 = new User();
        user1.setId("2");
        user1.setName("王五");
        user1.setAge(34);
        user1.setBirthday(new Date());
        user1.setSex(2);
        list.add(user1);

        User user3 = new User();
        user3.setId("3");
        user3.setName("李四");
        user3.setAge(25);
        user3.setBirthday(new Date());
        user3.setSex(3);
        list.add(user3);
        model.addAttribute("userList",list);

        User user2 = new User();
        user2.setId("111");
        user2.setName("张三");
        user2.setAge(12);
        user2.setBirthday(new Date());
        model.addAttribute("user",user2);
        return "demo";
    }

    @RequestMapping("/openid")
    @ResponseBody
    public Object getOpenId(String code) throws IOException {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxbf94850d9b5e5038&secret=2149538a29512a27a5d3c2a692504ce7&js_code="+code+"&grant_type"
                +"=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity entity = restTemplate.getForEntity(url,String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(entity.getBody());
        Map<String, Object> tmpMap=objectMapper.readValue(entity.getBody().toString(), Map.class);
        return tmpMap;
    }


    @RequestMapping("/getUser")
    @ResponseBody
    public Object getUser(String id,String name) throws IOException {
        System.out.println("id:"+id+",name="+name);
        Map<String,User> map = new HashMap<>();
        User u1 = new User();
        u1.setId("111");
        u1.setSex(23);
        u1.setBirthday(new Date());
        map.put("a",u1);
        map.put("b",u1);
        return new Date();
    }
}
